package rcpexample;

import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.jface.action.Separator;

/**
 * An action bar advisor is responsible for creating, adding, and disposing of
 * the actions added to a workbench window. Each window will be populated with
 * new actions.
 */
public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	// Actions - important to allocate these only in makeActions, and then use
	// them
	private IWorkbenchAction newAction;
	private IWorkbenchAction saveAction;
	private IWorkbenchAction saveAllAction;
	private IWorkbenchAction printAction;
	private IWorkbenchAction saveAsAction;
	private IWorkbenchAction closeAction;
	private IWorkbenchAction closeAllAction;

	// in the fill methods. This ensures that the actions aren't recreated
	// when fillActionBars is called with FILL_PROXY.

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	@Override
	protected void makeActions(IWorkbenchWindow window) {
		makeAction(window, ActionFactory.UNDO);
		makeAction(window, ActionFactory.REDO);
		makeAction(window, ActionFactory.COPY);
		makeAction(window, ActionFactory.PASTE);
		makeAction(window, ActionFactory.CUT);
		makeAction(window, ActionFactory.SELECT_ALL);
		makeAction(window, ActionFactory.DELETE);
		makeAction(window, ActionFactory.FIND);
		makeAction(window, ActionFactory.IMPORT);
		makeAction(window, ActionFactory.EXPORT);

		newAction = ActionFactory.NEW.create(window);
		register(newAction);
		saveAction = ActionFactory.SAVE.create(window);
		register(saveAction);
		saveAllAction = ActionFactory.SAVE_ALL.create(window);
		register(saveAllAction);
		printAction = ActionFactory.PRINT.create(window);
		register(printAction);
		saveAsAction = ActionFactory.SAVE_AS.create(window);
		register(saveAsAction);
		closeAction = ActionFactory.CLOSE.create(window);
		register(closeAction);
		closeAllAction = ActionFactory.CLOSE_ALL.create(window);
		register(closeAllAction);
	}

	protected IWorkbenchAction makeAction(IWorkbenchWindow window, ActionFactory af) {
		IWorkbenchAction action = af.create(window);
		register(action);
		return action;
	}

	

	@Override
	protected void fillMenuBar(IMenuManager menuBar) {	
		// fileMenu = new MenuManager("&File",MainMenu.FILE.getId() );        
		//fileMenu.add(new Separator());
		//fileMenu.add(closeAction);
		//fileMenu.add(closeAllAction);
		//fileMenu.add(new Separator());
		//fileMenu.add(saveAction);
		//fileMenu.add(saveAsAction);
		//fileMenu.add(saveAllAction);
		//fileMenu.add(new Separator());
		//fileMenu.add(printAction);
		//fileMenu.add(new Separator());
		//fileMenu.add(getAction(ActionFactory.IMPORT.getId()));
		//fileMenu.add(getAction(ActionFactory.EXPORT.getId()));    
		// menuBar.add(fileMenu);

		
		MenuManager editMenu = new MenuManager("&Edit", MainMenu.EDIT.getId());
		editMenu.add(getAction("undo"));
		editMenu.add(getAction("redo"));
		editMenu.add(getAction("find"));
		editMenu.add(getAction("selectAll"));
		editMenu.add(getAction("delete"));

		menuBar.add(editMenu);

	}	

	@Override
	public void fillActionBars(int flags) {
		super.fillActionBars(flags);
		IActionBarConfigurer configurer = getActionBarConfigurer();
		
		if ((flags & FILL_MENU_BAR) == 0)
			return;
		
		IWorkbenchWindow window = configurer.getWindowConfigurer().getWindow();
		IMenuManager mainMenu = configurer.getMenuManager();		
		MenuManager windowMenu = new MenuManager("&Window", IWorkbenchActionConstants.M_WINDOW);
		mainMenu.add(windowMenu);
		windowMenu.add(ActionFactory.MAXIMIZE.create(window));
		MenuManager helpMenu = new MenuManager("&Help", IWorkbenchActionConstants.M_HELP);
		mainMenu.add(helpMenu);
		// helpMenu.add(new AboutAction());

	}
}